/**
 * Created by smp on 2017/4/5.
 */
//初始化下拉树
$('.parentDepartmentId').combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});
//表单提交
$('#cardSave').on('click',function () {
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $('#cardForm').form('submit', {
            url: ROOT_PATH+'/observation/observationCard/add.shtml',

            onSubmit: function (param) {
                param.obsStatus="已登记";
                if( $("#obsFoundDate").datebox("getValue")==null|| $("#obsFoundDate").datebox("getValue")==''){
                    layer.close(index);
                    layer.msg('请填写日期', {
                        icon: 2,
                        time: 2000
                    });
                    return;
                }
                if($('#isConstrctor').combobox('getValue')==0){
                    if( $('#obsDeptId').textbox('getValue')==null|| $('#obsDeptId').textbox('getValue')==''){
                        layer.msg('请填写观察部门', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    param.obsDeptId= $('#obsDeptId').textbox('getValue');
                }else{
                    if( $('#contractor').val()==null|| $('#contractor').val()==''){
                        layer.msg('请填写字典名称', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    param.obsDeptId= $('#contractor').val();
                }
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success){
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.$('#dg1').datagrid('load');
                        parent.layer.closeAll(); //先得到当前iframe层的索引
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});
//表单提交
$('#cardSubmit').on('click',function () {
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $('#cardForm').form('submit', {
            url: ROOT_PATH+'/observation/observationCard/add.shtml',
            onSubmit: function (param) {
                param.obsStatus="等待分析";
                if( $("#obsFoundDate").datebox("getValue")==null|| $("#obsFoundDate").datebox("getValue")==''){
                    layer.close(index);
                    layer.msg('请填写日期', {
                        icon: 2,
                        time: 2000
                    });
                    return;
                }
                if($('#isConstrctor').combobox('getValue')==0){
                    if( $('#obsDeptId').textbox('getValue')==null|| $('#obsDeptId').textbox('getValue')==''){
                        layer.msg('请填写观察部门', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    param.obsDeptId= $('#obsDeptId').textbox('getValue');
                }else{
                    if( $('#contractor').val()==null|| $('#contractor').val()==''){
                        layer.msg('请填写字典名称', {
                            icon: 2,
                            time: 2000
                        });
                        return;
                    }
                    param.obsDeptId= $('#contractor').val();
                }
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (result) {
                result = $.parseJSON(result);
                if (result.success){
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:1},function () {
                        parent.$('#dg1').datagrid('load');
                        parent.layer.closeAll(); //先得到当前iframe层的索引
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});
//参数清空
$('#cardReset').on('click',function () {
    $('#cardForm').form('reset');
});

$(":input[name=isConstrctor]").combobox({
    onChange:function (newValue,oldValue) {
        if(newValue==0){
            $(".unit").css("display","block");
            $(".contractor").css("display","none");
        }else{
            $(".unit").css("display","none");
            $(".contractor").css("display","block");
        }
    }
});

$(".reaction-first .option").click(function () {
    if($(this).is(":checked")){
        $(this).parent("li").siblings("li").children(".option").attr("checked",false).attr("disabled",true);
    }else {
        $(this).parent("li").siblings("li").children(".option").attr("disabled",false);
    }

});

var tree=null;
var getSelectedUser = function (data) {
   if(tree=="analysisTree"){
        $("#analysis").val(data[0].id);
        $("#analysisTree").textbox("setValue",data[0].realName);
    }else{
        $("#obsReporter").val(data[0].id);
        $("#obsRep").textbox("setValue",data[0].realName);
    }
};
var getSelectedContractor=function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.name);
    $('#contractor').val(data.id);
};
$('#analysisTree').textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            tree="analysisTree";
            WindowManager.openLayer({
                type: 2,
                title: '添加分析人',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                callBack:getSelectedUser
            });
        }
    }],
    editable: false
});
$('#obsRep').textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            tree="obsRep";
            WindowManager.openLayer({
                type: 2,
                title: '添加报告人',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                callBack:getSelectedUser
            });
        }
    }],
    editable: false
});
$('#contractorName').textbox({
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            WindowManager.openLayer({
                type: 2,
                title: '选择承包商',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/contractor/contractorInfo/simpleList.shtml',
                callBack:getSelectedContractor
            });
        }
    }],
    editable: false
});